package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderDetailService orderDetailService;
    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
    /**
     * 分页查询订单
     * @param page
     * @param pageSize
     * @return
     */
//    @GetMapping("/userPage")
//    public R<Page> page(int page,int pageSize){
//        //分页构造器
//        Page<Orders> pageInfo = new Page<>(page,pageSize);
//        //条件构造器
//        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
//        //添加排序条件，根据sort进行排序
//        queryWrapper.orderByDesc(Orders::getCheckoutTime);
//
//        //进行分页查询
//        orderService.page(pageInfo,queryWrapper);
//
//        return R.success(pageInfo);
//    }
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize){
        //分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();


        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByDesc(Orders::getCheckoutTime);

        //进行分页查询
        orderService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list1 = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OrderDetail::getOrderId,item.getId());
            List<OrderDetail> list = orderDetailService.list(lambdaQueryWrapper);
            ordersDto.setOrderDetails(list);
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list1);


        return R.success(ordersDtoPage);
    }

    /**
     * 用户再次下单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> againSubmit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.againOrder(orders);
        return R.success("下单成功");

    }

    /**
     * 订单信息分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime,String endTime){
        //构造分页构造器对象
        Page<Orders> pageInfo = new Page<>(page,pageSize);
//        Page<OrdersDto> ordersDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(number != null,Orders::getNumber,number)
                .between((beginTime != null && endTime != null) ,Orders::getOrderTime,beginTime,endTime)
                .orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo, queryWrapper);

        List<Orders> records = pageInfo.getRecords();

        List<Orders> list = records.stream().map((item) -> {
            Long userId = item.getUserId();
            User user = userService.getById(userId);
            item.setUserName(user.getName());

            return item;
        }).collect(Collectors.toList());

        pageInfo.setRecords(list);

        return R.success(pageInfo);
    }

    /**
     * 派送订单
     * @return
     */
    @PutMapping
    public R<String> arrived(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Orders::getStatus,orders.getStatus()).eq(Orders::getId,orders.getId());
        orderService.update(updateWrapper);
        return R.success("订单状态改变成功");
    }

}